#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>



int main()
{
    int fd=open("bite", O_RDWR | O_CREAT|O_TRUNC, 0644); // 打开文件，读写模式
    if(fd<0)
    {
        perror("open failed");
        return 1;
    }
    // 写入数据
    const char *data = "i like linux!";
    size_t len = strlen(data);
    ssize_t written = write(fd, data, len); // 按字节写入
    if(written<0)
    {
        perror("write failed");
        close(fd);
        return 1;
    }
    printf("Written bytes: %zd\n", written);
    // 读取数据
    lseek(fd, 0, SEEK_SET); // 重置文件指针到开头
    char buffer[1024] = {0};
    ssize_t read_count = read(fd, buffer, sizeof(buffer)-1); // 预留'\0'位置
    if(read_count<0)
    {
        perror("read failed");
        close(fd);
        return 1;
    }
    buffer[read_count] = '\0'; // 确保字符串终止
    printf("Read count: %zd\n", read_count);
    printf("File Content: %s\n", buffer);
    // 关闭文件描述符
    close(fd);
    return 0;
    
}